﻿<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace=" System.Collections" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="TarreOA.TarreOAModels" %>
<%@ Import Namespace="TarreOA.TarreOABLL" %>

<script runat="server">

    //private string cP;
    private string cataloagName;

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            if(Request["c_N"] != null && Request.QueryString.Get("c_id") != null)
            {
                cataloagName = Request["c_N"];
                this.btnBack.Attributes.Add("c_id", Request.QueryString.Get("c_id"));
                this.findAllPath();
                ViewState["catalogName"] = cataloagName;
            }
        }
    }

    protected void findAllPath()
    {
        this.DdlParentPath.Items.Clear();
        NetDiskCatalogFolderAndFileService ndcfafService = new NetDiskCatalogFolderAndFileService();
        List<string> list = ndcfafService.findCurrentCatalogAllFolder(FindConfigurationPath.instance().NetDiskRoots[cataloagName].ToString());
        ListItem rootItem = new ListItem("\\", "");
        this.DdlParentPath.Items.Add(rootItem);
        if(list != null)
        {
            foreach(string pathAndName in list)
            {
                string[] pan = pathAndName.Split(',');
                ListItem listItem = new ListItem(pan[1], pan[0].Replace(FindConfigurationPath.instance().NetDiskRoots[cataloagName].ToString(), ""));
                this.DdlParentPath.Items.Add(listItem);
            }
        }
    }

    protected void btnBack_ServerClick(object sender, EventArgs e)
    {
        if(ViewState["catalogName"] != null)
        {
            cataloagName = (string) ViewState["catalogName"];
        }
        string c_id = ((HtmlInputSubmit) sender).Attributes["c_id"];
        Response.Redirect("NetDiskCatalogFolderShow.aspx?C_N=" + cataloagName + "&C_ID=" + c_id);
    }

    protected void btnSubmit_ServerClick(object sender, EventArgs e)
    {
        if(ViewState["catalogName"] != null)
        {
            cataloagName = (string) ViewState["catalogName"];
        }
        NetDiskCatalogFolderAndFileService ndcFaFService = new NetDiskCatalogFolderAndFileService();
        string path = FindConfigurationPath.instance().NetDiskRoots[cataloagName].ToString() + Request.Params.GetValues(this.DdlParentPath.ClientID)[0];
        List<NetDiskCatalogFolderAndFile> list = ndcFaFService.findFilesAtThePath(path, Request[this.txtKey.ClientID]);
        if(list != null && list.Count > 0)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("FolderOrFileName", typeof(string));
            dt.Columns.Add("FolderOrFilePath", typeof(string));
            dt.Columns.Add("FolderOrFileCreateTime", typeof(string));
            dt.Columns.Add("FolderOrFile", typeof(string));
            dt.Columns.Add("FolderOrFileSize", typeof(string));
            foreach(NetDiskCatalogFolderAndFile ndcfaf in list)
            {
                DataRow dr = dt.NewRow();
                dr["FolderOrFileName"] = ndcfaf.FolderOrFileName;
                dr["FolderOrFilePath"] = ndcfaf.FolderOrFilePath.Replace(FindConfigurationPath.instance().NetDiskRoots[cataloagName].ToString(), "");
                dr["FolderOrFileCreateTime"] = ndcfaf.FoderOrFileCreateTime;
                dr["FolderOrFile"] = ndcfaf.FolderOrFile;
                dr["FolderOrFileSize"] = ndcfaf.FolderOrFileSize;
                dt.Rows.Add(dr);
            }
            this.DataList1.DataSource = dt.DefaultView;
            this.DataList1.DataBind();
        }
        else
        {
            this.DataList1.DataSource = null;
            this.DataList1.DataBind();
        }
        ViewState["catalogName"] = cataloagName;
    }

    private int fileCount;
    protected void DataList1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            string curPath = ((HtmlInputHidden) e.Item.FindControl("HidcP")).Value;
            ((LinkButton) e.Item.FindControl("LinkButton1")).Attributes.Add("cP", curPath);
            ((ImageButton) e.Item.FindControl("imBtnProps")).Attributes.Add("cP", curPath);
            fileCount++;
            ((Image) e.Item.FindControl("Image1")).Visible = false;
            ((ImageButton) e.Item.FindControl("imgBtnDownLoad")).Attributes.Add("cP", curPath);
        }
        if(e.Item.ItemType == ListItemType.Footer)
        {
            ((Label) e.Item.FindControl("Label1")).Text = "共" + fileCount.ToString() + "个文件";
        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if(ViewState["catalogName"] != null)
        {
            cataloagName = (string) ViewState["catalogName"];
        }
        string filePath = FindConfigurationPath.instance().NetDiskRoots[cataloagName].ToString() + ((LinkButton) sender).Attributes["cP"];
        string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
        FileInfo fi = new FileInfo(filePath);
        if(fi.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment;fileName=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.ContentType = "application/octet-stream";
            Response.Filter.Close();
            Response.WriteFile(fi.FullName);
            Response.End();
        }
        ViewState["catalogName"] = cataloagName;
    }

    protected void imBtnProps_Click(object sender, ImageClickEventArgs e)
    {
        if(ViewState["catalogName"] != null)
        {
            cataloagName = (string) ViewState["catalogName"];
        }
        string c_P = ((ImageButton) sender).Attributes["cP"];
        Response.Redirect("FolderProps.aspx?c_P=" + c_P + "&c_N=" + cataloagName);
    }

    protected void imgBtnDownLoad_Click(object sender, ImageClickEventArgs e)
    {
        if(ViewState["catalogName"] != null)
        {
            cataloagName = (string) ViewState["catalogName"];
        }
        string filePath = FindConfigurationPath.instance().NetDiskRoots[cataloagName].ToString() + ((ImageButton) sender).Attributes["cP"];
        string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
        FileInfo fi = new FileInfo(filePath);
        if(fi.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment;fileName=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.ContentType = "application/octet-stream";
            Response.Filter.Close();
            Response.WriteFile(fi.FullName);
            Response.End();
        }
        ViewState["catalogName"] = cataloagName;
    }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>

    
                <link rel="Stylesheet" type="text/css" href="../Styles/right_style.css" />
                <script type="text/javascript" src="../Scripts/jquery.ui/js/jquery.js"></script>

                <style type="text/css">
                <!--
                html{ scrollbar-base-color: #dbecf8; scrollbar-arrow-color: #2a8ed1; scrollbar-track-color: #bdddf2; scrollbar-3dlight-color: #2886c5; scrollbar-darkshadow-color: #2886c5;
                scrollbar-face-color: #dbecf8; scrollbar-shadow-color: #dbecf8;  }
                -->
                </style>


</head>
<body>
    <form id="form1" runat="server">
        <div class="sub_right_top">
  <div class="hdm_header" >
    <div class="tb1_on"><h2 class="h2_icon">网络硬盘</h2></div>

    <div class="clear"></div>
  </div>
  <div class="hdm_footer">
    <div style="height:13px; overflow:hidden;"></div>

        <table width="97%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#77acd0" style="border-collapse:collapse;">
            <tr bgcolor="#b1d5ef">
                <td colspan="2" nowrap="nowrap">
                    &nbsp;&nbsp;<img align="absMiddle" border="0" src="../images/report_search.gif">&nbsp;<font
                        style="font-size: 12px">请设定搜索条件</font></td>
            </tr>
            <tr class="TableData" bgcolor="white">
                <td align="right" nowrap="nowrap">
                    &nbsp;&nbsp;&nbsp;&nbsp;<font style="font-size: 12px">关键字：</font></td>
                <td>
                    <input id="txtKey" class="BigInput" maxlength="30" name="txtKey" size="30" type="text"
                        runat="server" /></td>
            </tr>
            <tr class="TableData" bgcolor="white">
                <td align="right" nowrap="nowrap">
                    <font style="font-size: 12px">位置：</font></td>
                <td>
                    <asp:DropDownList ID="DdlParentPath" runat="server" Width="215px">
                    </asp:DropDownList></td>
            </tr>
            <tr class="TableControl">
                <td align="center" colspan="2" style="height: 30px">
                    <input id="btnSubmit" class="BigButton" name="btnSubmit" onclick="" type="submit"
                        value="搜索" runat="server" onserverclick="btnSubmit_ServerClick" />&nbsp;&nbsp;
                    <input id="btnBack" class="BigButton" name="btnBack" type="submit" value="返回" runat="server"
                        onserverclick="btnBack_ServerClick" /></td>
            </tr>
        </table>
        <br />
                    <asp:Repeater ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
                        <HeaderTemplate>
                            <table width="97%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#77acd0" style="border-collapse:collapse;">
                                 <tr bgcolor="#b1d5ef">
                                <td align="center" nowrap="nowrap">
                                    <font style="font-size: 12px"><b>名称</b></font></td>
                                <td align="center" nowrap="nowrap">
                                    <font style="font-size: 12px"><b>大小</b></font></td>
                                <td align="center" nowrap="nowrap">
                                    <font style="font-size: 12px"><b>最后修改时间</b></font></td>
                                <td align="center" nowrap="nowrap" width="63px">
                                    <font style="font-size: 12px"><b>操作</b></font></td>
                            </tr>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <tr>
                                <td nowrap="nowrap" style="height: 25px">
                                    <asp:Image ID="Image1" runat="server" Height="21px" ImageAlign="AbsMiddle" Width="19px" />
                                    <asp:LinkButton ID="LinkButton1" runat="server"
                                        Font-Underline="False" OnClick="LinkButton1_Click" Text='<%# Eval("FolderOrFileName") %>'
                                        Width="163px"></asp:LinkButton></td>
                                <td align="center" nowrap="nowrap" style="width: 108px; height: 25px">
                                    <font style="font-size: 12px">
                                        <%# Eval("FolderOrFileSize") %>
                                    </font>
                                </td>
                                <td align="center" nowrap="nowrap" style="width: 156px; height: 25px">
                                    <font style="font-size: 12px">
                                        <%# Eval("FolderOrFileCreateTime") %>
                                    </font>
                                </td>
                                <td align="center" nowrap="nowrap" style="width: 63px; height: 25px">
                                    <font style="font-size: 12px">
                                        <asp:ImageButton ID="imBtnProps" runat="server" ImageAlign="AbsMiddle"
                                            ImageUrl="~/images/props.gif" OnClick="imBtnProps_Click" ToolTip="查看属性"/>
                                        <asp:ImageButton ID="imgBtnDownLoad" runat="server" ImageAlign="AbsMiddle"
                                            ImageUrl="~/images/down.gif" OnClick="imgBtnDownLoad_Click" ToolTip="下载" />
                                    </font>
                                </td>
                            </tr>
                            <input id="HidcP" runat="server" type="hidden" value='<%# Eval("FolderOrFilePath") %>' />
                        </ItemTemplate>
                        
                        <FooterTemplate>
                            <tr bgcolor="white">
                                <td align="left" colspan="4" nowrap="nowrap" style="height: 25px">
                                    <font style="font-size: 12px">
                                        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>&nbsp; </font>
                                </td>
                            </tr>
                            </table>
                        </FooterTemplate>
                    </asp:Repeater>
    </form>
</body>
</html>
